package com.elitel.frame.base.service.impl;

import com.elitel.common.utils.UUIDUtil;
import com.elitel.frame.base.service.AcAppStationService;
import com.elitel.frame.base.service.ApplicationServiceAccessConfigService;
import com.elitel.frame.main.dao.*;
import com.elitel.frame.main.dao.ext.ExtSysPrjserviceMapper;
import com.elitel.frame.main.entity.*;
import com.elitel.frame.main.entity.acappstation.AcAppStation;
import com.elitel.frame.main.entity.acappstation.AcAppStationExample;
import com.elitel.frame.main.entity.constant.TmpUser;
import com.elitel.frame.main.entity.dto.AcAppServiceRelationDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author hi_bj
 */
@Service
public class ApplicationServiceAccessConfigServiceImpl implements ApplicationServiceAccessConfigService {

    @Resource
    AcAppMapper acAppMapper;

    @Resource
    AcAppServiceMapper acAppServiceMapper;

    @Resource
    private AcAppStationMapper acAppStationMapper;

    @Resource
    private ExtSysPrjserviceMapper extSysPrjserviceMapper;

    @Override
    public List<AcAppServiceRelation> getDataList(AcAppServiceRelation acAppServiceRelation) {
        List<AcAppServiceRelation> resultList = new ArrayList<>();
        List<AcApp> acAppList = acAppMapper.getAll(null);
        List<AcAppService> acAppServiceList = acAppServiceMapper.getAllByServiceId(acAppServiceRelation.getServiceId());
        if (ObjectUtils.isEmpty(acAppServiceList)) {
            SysPrjservice sysPrjservice = extSysPrjserviceMapper.getProjectServiceByServiceCode(acAppServiceRelation.getServiceId());
            if (sysPrjservice == null) {
                return resultList;
            }
            acAppServiceList = acAppServiceMapper.getAllByServiceId(sysPrjservice.getGuid());
        }
        acAppServiceList.forEach(service -> {
            acAppList.forEach(app -> {
                if (app.getAppId().equals(service.getAppId())) {
                    AcAppServiceRelation appServiceRelation = new AcAppServiceRelation();
                    BeanUtils.copyProperties(service, appServiceRelation);
                    appServiceRelation.setAppCode(app.getAppCode());
                    appServiceRelation.setAppName(app.getAppName());
                    appServiceRelation.setDepartment(app.getDepartment());
                    resultList.add(appServiceRelation);
                }
            });
        });
        return resultList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(AcAppServiceRelationDTO dto) {
        AtomicInteger count = new AtomicInteger();
        String serviceId = dto.getServiceId();
        // 传入的数据为空 就删除之前所有的
        List<AcAppServiceRelation> acAppServiceRelationList = dto.getAcAppServiceRelation();
        acAppServiceMapper.deleteByServiceId(serviceId);


        acAppServiceRelationList.forEach(service -> {
            AcAppService acAppService = new AcAppService();
            acAppService.setAppServiceId(UUIDUtil.randomUUID());
            acAppService.setServiceId(service.getServiceId());
            acAppService.setAppId(service.getAppId());
            acAppService.setCreatedTime(new Date());
            acAppService.setCreatedUser(TmpUser.ADMIN);
            acAppService.setStartTime(service.getStartTime());
            acAppService.setEndTime(service.getEndTime());
            acAppService.setStartHour(service.getStartHour());
            acAppService.setEndHour(service.getEndHour());
            acAppService.setDailyCount(service.getDailyCount());
            acAppService.setFilterCatalogId(service.getFilterCatalogId());
            acAppService.setFilterStartTime(service.getFilterStartTime());
            acAppService.setFilterEndTime(service.getFilterEndTime());
            count.addAndGet(acAppServiceMapper.insertSelective(acAppService));
        });
        return count.get();
    }


    /**
     * 获取目标业务库中新增的测站数据
     *
     * @param acAppServiceRelationList
     * @return
     */
    @Override
    public List<AcFilterStation> getInsertStationParams(List<AcAppServiceRelation> acAppServiceRelationList) {
        List<AcFilterStation> resList = new ArrayList<>();

        if (CollectionUtils.isEmpty(acAppServiceRelationList)) {
            return resList;
        }
        // 获取分组id
        List<String> catalogIds = acAppServiceRelationList.stream()
                .map(AcAppServiceRelation::getFilterCatalogId)
                .filter(StringUtils::isNotEmpty)
                .distinct()
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(catalogIds)) {
            return resList;
        }

        // 根据分组查询分组下面的测站
        AcAppStationExample example = new AcAppStationExample();
        example.createCriteria().andCatalogIdIn(catalogIds);
        List<AcAppStation> acAppStations = acAppStationMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(acAppStations)) {
            return resList;
        }

        // 根据分组id将测站进行分组
        Map<String, List<AcAppStation>> catalogIdStationsMap = acAppStations.stream().collect(Collectors.groupingBy(AcAppStation::getCatalogId));

        // 封装推送的测站信息
        for (AcAppServiceRelation appServiceRelation : acAppServiceRelationList) {
            String catalogId = appServiceRelation.getFilterCatalogId();
            List<AcAppStation> stations = catalogIdStationsMap.get(catalogId);

            if (CollectionUtils.isEmpty(stations)) {
                continue;
            }

            for (AcAppStation station : stations) {
                AcFilterStation acFilterStation = new AcFilterStation();
                acFilterStation.setDataId(UUIDUtil.randomUUID());
                acFilterStation.setServiceId(appServiceRelation.getServiceId());
                acFilterStation.setStcd(station.getStcd());
                acFilterStation.setAppCode(appServiceRelation.getAppCode());
                resList.add(acFilterStation);
            }
        }
        return resList;
    }


}
